<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>首页信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>合作单位列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getCooperationList" @keyup.enter="getCooperationList">
            <el-button slot="append" icon="el-icon-search" @click="getCooperationList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addCooperationDialogVisible = true">添加合作单位</el-button>
        </el-col>
      </el-row>

      <el-table :data="CooperationList" border stripe>
        <el-table-column label="合作单位" prop="co_name"></el-table-column>
        <el-table-column label="简介" prop="co_introduce"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <!--<el-button type="primary" icon="el-icon-edit" size="mini" @click="showeditCooperation(scope.row.id)"></el-button>-->
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeCooperationById(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNo"
        :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加合作单位的对话框 -->
    <el-dialog title="添加合作单位" :visible.sync="addCooperationDialogVisible" width="50%" @close="addCooperationDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addCooperationForm" :rules="addCooperationFormRules" ref="addCooperationFormRef" label-width="70px">
        <el-form-item label="合作单位" prop="co_name">
          <el-input v-model="addCooperationForm.co_name"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="co_introduce">
          <el-input v-model="addCooperationForm.co_introduce"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <input type="file" @change="getFileadd($event)" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCooperationDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCooperation">确 定</el-button>
      </span>

    </el-dialog>

    <!-- 修改合作单位的对话框 -->
    <el-dialog title="修改管理员信息" :visible.sync="editCooperationVisible" width="50%" @close="editCooperationClosed">
      <el-form :model="editCooperationForm" :rules="editCooperationFormRules" ref="editCooperationFormRef" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="editCooperationForm.title"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="editCooperationForm.info"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="editCooperationForm.content"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="editCooperationForm.author"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <input type="file" @change="getFileedit($event)" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCooperationVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCooperation">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    data() {

      return {
        // 获取合作单位列表的参数对象
        queryInfo: {
          pageNo: 1,
          // 当前每页显示多少条数据
          pageSize: 5
        },
        CooperationList: [],
        total: 0,
        // 控制添加合作单位对话框的显示与隐藏
        addCooperationDialogVisible: false,
        // 添加合作单位的表单数据
        addCooperationForm: {
          id: '',
          co_name: '',
          co_introduce: '',
          file: ''
        },
        fileList: [],
        // 添加表单的验证规则对象
        addCooperationFormRules: {

        },
        // 控制修改合作单位对话框的显示与隐藏
        editCooperationVisible: false,
        // 查询到的合作单位信息对象
        editCooperationForm: {},
        // 修改表单的验证规则对象
        editCooperationFormRules: {

        },
        // 所有角色的数据列表
        rolesList: [],
        userInfo: {},
        // 已选中的角色Id值
        selectedRoleId: ''
      }
    },
    created() {
      this.getCooperationList()
    },
    methods: {
      async getCooperationList() {
        const {
          data: res
        } = await this.$http.get('cooperation', {
          params: this.queryInfo
        })

        if (res.code !== 200) {
          return this.$message.error('获取合作单位列表失败！')
        }
        this.CooperationList = res.data.info
        this.total = res.data.total
        console.log(this.CooperationList)
      },
      // 监听 pageSize 改变的事件
      handleSizeChange(Cooperationize) {
        this.queryInfo.pageSize = Cooperationize
        this.getCooperationList()
      },
      // 监听 页码值 改变的事件
      handleCurrentChange(newPage) {
        this.queryInfo.pageNo = newPage
        this.getCooperationList()
      },
      // 监听添加合作单位对话框的关闭事件
      addCooperationDialogClosed() {
        this.$refs.addCooperationFormRef.resetFields()
      },
      // 点击按钮，添加新合作单位
      addCooperation() {
        let form = new FormData()
        form.append('mfile', this.addCooperationForm.file)
        form.append('co_name', this.addCooperationForm.co_name)
        form.append('co_introduce', this.addCooperationForm.co_introduce)
        form.append('id', this.addCooperationForm.id)
        this.$refs.addCooperationFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$http.post('/cooperation', form, {
            headers: {
              'Content-Type': 'multipart/form-data'
            },
          })

          if (res.code !== 200) {
            this.$message.error('添加合作单位失败！')
          }

          this.$message.success('添加合作单位成功！')
          // 隐藏添加合作单位的对话框
          this.addCooperationDialogVisible = false
          // 重新获取合作单位列表数据
          this.getCooperationList()
        })
      },
      getFileadd(event) {
        this.addCooperationForm.file = event.target.files[0]

      },
      // 展示编辑合作单位的对话框
      async showeditCooperation(id) {
        const {
          data: res
        } = await this.$http.get('cooperation/' + id)

        if (res.code !== 200) {
          return this.$message.error('查询合作单位信息失败！')
        }
        c
        this.editCooperationForm = res.data

        this.editCooperationVisible = true
      },
      // 监听修改合作单位对话框的关闭事件
      editCooperationClosed() {
        this.$refs.editCooperationFormRef.resetFields()
      },
      // 修改合作单位信息并提交
      editCooperation() {
        let form = new FormData()
        form.append('file', this.editCooperationForm.file)
        form.append('co_name', this.editCooperationForm.co_name)
        form.append('co_introduce', this.editCooperationForm.co_introduce)
        form.append('mfile', this.editCooperationForm.mfile)
        this.$refs.editCooperationFormRef.validate(async valid => {
          if (!valid) return
          // 发起修改合作单位信息的数据请求
          const {
            data: res
          } = await this.$http.put('cooperation/', form, {
            headers: {
              'Content-Type': 'multipart/form-data'
            },
          })

          if (res.meta.status !== 200) {
            return this.$message.error('更新合作单位信息失败！')
          }

          // 关闭对话框
          this.editCooperationVisible = false
          // 刷新数据列表
          this.getCooperationList()
          // 提示修改成功
          this.$message.success('更新合作单位信息成功！')
        })
      },
      // 根据Id删除对应的合作单位信息
      async removeCooperationById(id) {
        // 弹框询问合作单位是否删除数据
        const confirmResult = await this.$confirm(
          '此操作将永久删除该合作单位, 是否继续?',
          '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).catch(err => err)

        // 如果合作单位确认删除，则返回值为字符串 confirm
        // 如果合作单位取消了删除，则返回值为字符串 cancel
        // console.log(confirmResult)
        if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除')
        }

        const {
          data: res
        } = await this.$http.delete('cooperation/' + id) 
        
        if (res.code !== 200) {
          return this.$message.error('删除合作单位失败！')
        }

        this.$message.success('删除合作单位成功！')
        this.getCooperationList()
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
